package com.sum.tea.xinlian

import android.app.Dialog
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.SimpleAdapter
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.R
import com.sum.tea.contants.AppConfig
import com.sum.tea.databinding.ActivityRegisteropBinding
import com.sum.tea.xinlian.dialog.OpenFileDialog
import com.sum.tea.xinlian.viewmodel.SubRegopViewModel
import kotlinx.coroutines.launch

class SubRegopActivity : BaseMvvmActivity<ActivityRegisteropBinding, SubRegopViewModel>() {
    private val openfileDialogId = 0

    override fun initView(savedInstanceState: Bundle?) {
        // 初始化 Spinner
        val adapter = ArrayAdapter(
            this,
            android.R.layout.simple_spinner_item,
            AppConfig.regtype
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.spinnerRegtype.adapter = adapter

        // 设置按钮点击事件
        mBinding.buttonFileread.setOnClickListener {
            showDialog(openfileDialogId)
        }

        mBinding.buttonRegread.setOnClickListener {
            readButtonClicked()
        }

        mBinding.buttonRegwrite.setOnClickListener {
            writeButtonClicked()
        }

        // 观察 ViewModel 的操作结果
        mViewModel.operationResult.observe(this) { result ->
            Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
        }

        mViewModel.regList.observe(this) { list ->
            val adapter = SimpleAdapter(this@SubRegopActivity, list, R.layout.listitemview_reg,
                arrayOf("number", "address", "value"), intArrayOf(
                    R.id.textView_regnumber, R.id.textView_regaddress,
                    R.id.textView_regval
                )
            )
            mBinding.listViewReglist.adapter = adapter
        }
    }

    private fun readButtonClicked() {
        val selectedType = mBinding.spinnerRegtype.selectedItemPosition
        val readAddress = mBinding.editTextRegadr.text.toString()
        val readCount = mBinding.editTextRegrc.text.toString()

        if (selectedType == -1) {
            Toast.makeText(this, "请选择类型", Toast.LENGTH_SHORT).show()
            return
        }

        if (readAddress.isEmpty()) {
            Toast.makeText(this, "请输入读地址", Toast.LENGTH_SHORT).show()
            return
        }

        lifecycleScope.launch {
            mViewModel.readRegister(
                selectedType,
                readAddress,
                readCount
            )
        }
    }

    private fun writeButtonClicked() {
        val selectedType = mBinding.spinnerRegtype.selectedItemPosition
        val writeAddress = mBinding.editTextRegwadr.text.toString()
        val writeValue = mBinding.editTextRegwv.text.toString()

        if (selectedType == -1) {
            Toast.makeText(this, "请选择类型", Toast.LENGTH_SHORT).show()
            return
        }

        if (writeAddress.isEmpty()) {
            Toast.makeText(this, "请输入写地址", Toast.LENGTH_SHORT).show()
            return
        }

        lifecycleScope.launch {
            mViewModel.writeRegister(
                selectedType,
                writeAddress,
                writeValue
            )
        }
    }

    override fun onCreateDialog(id: Int): Dialog? {
        return if (id == openfileDialogId) {
            val images = mapOf(
                OpenFileDialog.sRoot to R.drawable.filedialog_root,
                OpenFileDialog.sParent to R.drawable.filedialog_folder_up,
                OpenFileDialog.sFolder to R.drawable.filedialog_folder,
                "csv" to R.drawable.filedialog_file,
                "xlsx" to R.drawable.filedialog_file,
                "xls" to R.drawable.filedialog_file,
                "txt" to R.drawable.filedialog_file,
                OpenFileDialog.sEmpty to R.drawable.filedialog_root
            )

            OpenFileDialog.createDialog(
                id,
                this,
                "打开文件",
                { bundle ->
                    val filepath = bundle.getString("path") ?: return@createDialog
                    lifecycleScope.launch {
                        mViewModel.readExcelFile(filepath)
                    }
                },
                null,
                images
            )
        } else {
            super.onCreateDialog(id)
        }
    }
}
